﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;


namespace HelpDesk.Models
{
    public class HelpDeskDbContext : DbContext
    {
        private static volatile HelpDeskDbContext instance;
        private static object syncRoot = new Object();

        public HelpDeskDbContext()
            : base("DefaultConnection")
        {
        }
        
        //private HelpDeskDbContext()
        //    : base("DefaultConnection")
        //{
        //}

        public static HelpDeskDbContext Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                            instance = new HelpDeskDbContext();
                    }
                }

                return instance;
            }
        } 

        public DbSet<UserProfile> UserProfiles { get; set; }
        public DbSet<Activity> Activities { get; set; }
        public DbSet<Department> Departments { get; set; }
        public DbSet<Ticket> Tickets { get; set; }
        public DbSet<TicketPriority> TicketPriorities { get; set; }
        public DbSet<TicketStatus> TicketStatus { get; set; }

        public static string ParseModelStateErrors(ICollection<ModelState> modelStateCollection,  string strHtmlTag = "li")
        {
            StringBuilder sb = new StringBuilder();

            if (modelStateCollection != null)
            {
                strHtmlTag.Replace("<", "").Replace("/", "").Replace(">", "");
                

                foreach (ModelState state in modelStateCollection)
                {
                    foreach (ModelError error in state.Errors)
                    {
                        sb.Append(string.Format("<{0}>{1}</{2}>", strHtmlTag, error.ErrorMessage, strHtmlTag));

                    }

                }
            }
            return sb.ToString();
        }

    }
}